package com.qijian.flowable.common.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.flowable.constant.FlowableConstant;
import com.qijian.flowable.domain.FlowableRecordPower;
import com.qijian.flowable.domain.query.FlowableRecordPowerQuery;
import com.qijian.flowable.service.IFlowableRecordPowerService;
import com.qijian.flowable.util.SpringContextUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl;
import org.flowable.engine.task.Comment;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 全局监听器，判断流程是不是运行到了最后一个EndEvent
 *
 * @author qijian
 */
@Component
public class TaskEndListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        // 当前节点任务实体,
        TaskEntity taskEntity;
        try {
            taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
        } catch (Exception e) {
            taskEntity = (TaskEntity) ((FlowableEntityWithVariablesEventImpl) event).getEntity();
        }
        if (!FlowableConstant.INITIATOR.equals(taskEntity.getTaskDefinitionKey()) && StringUtils.isNotBlank(taskEntity.getAssignee())) {
            // 审批之后添加查看权限
            TaskService taskService = SpringContextUtils.getBean(TaskService.class);
            List<Comment> datas = taskService.getProcessInstanceComments(taskEntity.getProcessInstanceId());
            if (CollectionUtil.isNotEmpty(datas)) {
                Comment component = datas.get(0);
                IFlowableRecordPowerService flowableRecordPowerService = SpringContextUtils.getBean(IFlowableRecordPowerService.class);
                FlowableRecordPowerQuery criteria = new FlowableRecordPowerQuery();
                criteria.setUserId(Long.parseLong(component.getUserId()));
                criteria.setProcessIntaneId(taskEntity.getProcessInstanceId());
                if (flowableRecordPowerService.count(WhereEntityTool.invoke(criteria)) == 0) {
                    FlowableRecordPower recordPower = new FlowableRecordPower();
                    recordPower.setProcessIntaneId(taskEntity.getProcessInstanceId());
                    recordPower.setType(component.getType());
                    recordPower.setUserId(Long.parseLong(component.getUserId()));
                    flowableRecordPowerService.save(recordPower);
                }
            }
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
